import Flutter
import UIKit

public class SwiftUmengLinkPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "umeng_link_plugin", binaryMessenger: registrar.messenger())
        let instance = SwiftUmengLinkPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        if "getUMLinkGetInstallParams" == call.method {
            UMengLinkManager.getInstallParamsWith(methodCall: call, result: result)
        } else {
            result(FlutterMethodNotImplemented);
        }
    }
    
    
    
    // MARK: 必须要实现的三个系统方法
    // Universal link的回调
    public func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]) -> Void) -> Bool {
        if MobClickLink.handleUniversalLink(userActivity, delegate: nil) {
            return true
        }
        return true
    }
    // URL Scheme回调，iOS9以上，走这个方法
    public func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if MobClickLink.handle(url, delegate: nil) {
            return true
        }
        return true
    }
    // URL Scheme回调，iOS9以下，走这个方法
    public func application(_ application: UIApplication, open url: URL, sourceApplication: String, annotation: Any) -> Bool {
        if MobClickLink.handle(url, delegate: nil) {
            return true
        }
        return true
    }
    
}
